const { envList } = require("../../envList");
const { QuickStartPoints, QuickStartSteps } = require("./constants");

Page({
  data: {
    calorieData: '2000',
    waterData: '2000',
    habitData: '50',
    exerciseData: '30',
    knowledgePoints: QuickStartPoints,
    steps: QuickStartSteps,
    swiperlist:[
        '../../images/健身-1.jpg',
        '../../images/健身-2.jpg',
        '../../images/健身-3.jpg'
    ],
    weeklist:['周一','周二','周三','周四','周五','周六','周日'],
    daylist:'',
    tablist:[
        {
            imgs:'../../images/跑步.png',
            name:"健康运动",
            pagepath:'../sports/index'
        },
        {
            imgs:'../../images/喝水.png',
            name:"喝水记录",
            pagepath:'../water/index'
        },
        {
            imgs:'../../images/饮食.png',
            name:"健康饮食",
            pagepath:'../heafood/index'
        },
        {
            imgs:'../../images/周期分析.png',
            name:"生理周期",
            pagepath:'../biology/index'
        },
        {
            imgs:'../../images/知识库.png',
            name:"知识头条",
            pagepath:'../knowledge/index'
        },
        {
            imgs:'../../images/课程.png',
            name:"课程推荐",
            pagepath:'../course/index'
        },
        {
            imgs:'../../images/管理健身计划.png',
            name:"健康测评",
            pagepath:'../report/index'
        },
        {
            imgs:'../../images/报告.png',
            name:"健康周报",
            pagepath:'../record/index'
        }
    ],
    dayindex:0,
    day:'',
    targetweight:'50.0',
    dayweight:'52.0',
    openid:''
  },
   //监听页面显示
 onShow(){
    if(typeof this.getTabBar==='function'&&this.getTabBar()){
          this.getTabBar().setData({
                    select:0
                })
        }
    },
      goPath(e){
       const {pagepath}=e.currentTarget.dataset
       wx.navigateTo({
         url: pagepath,
       })
      },
      onLoad(){
      this.updateWeekDays()
      this.getweight(this.data.day)
    //   this.addweight()
      },
      addweight(){
      
       wx.cloud.callFunction({
           name:'changeweight',
           data:{
               caloriedata:this.data.calorieData,
               day:this.data.day,
               exercisedata:this.data.exerciseData,
               habitdata:this.data.habitData,
               openid:wx.getStorageSync('openid'),
               waterdata:this.data.waterData,
               weightday:this.data.dayweight,
               weighttarget:this.data.targetweight
           },
           success:res=>{
               console.log(res)
           }
       })
      },
      getweight(date){
      wx.cloud.callFunction({
          name:'getindexdetail',
          data:{
              openid:wx.getStorageSync('openid'),
              day:date
          },
          success:res=>{
              console.log(res)
              if(res.result.data.length===0){
                this.addweight()
              }else{
                  this.setData({
                      targetweight:res.result.data[0].targetweight,
                      calorieData:res.result.data[0].caloriedata,
                      exerciseData:res.result.data[0].exercisedata,
                      habitData:res.result.data[0].habitdata,
                      waterData:res.result.data[0].waterdata,
                      dayweight:res.result.data[0].weightday
                  })
              }
          },
          fail:err=>{
              console.log('请更新今日数据')
          }
      })
      },
      gotoday(){
      const date=new Date()
      const day=date.getDate()
      const index=date.getDay()
      this.getweight(day)
      this.setData({
          dayindex:index===0?6:index-1
      })
      },
      changedetail(e){
       console.log(e)
       const {index,date}=e.currentTarget.dataset
       this.getweight(date)
       this.setData({
           dayindex:index
       })
      },
      updateWeekDays() {
        const currentDate = new Date();
        const currentDayIndex = currentDate.getDay(); // 0 (Sun) to 6 (Sat)
        const startOfWeek = new Date(currentDate);
        startOfWeek.setDate(currentDate.getDate() - currentDayIndex + 1); 
    
        const weekDays = Array.from({ length: 7 }, (_, i) => {
          const date = new Date(startOfWeek);
          date.setDate(startOfWeek.getDate() + i);
          return {
            date: date.getDate(),
          };
        });

        this.setData({
            daylist:weekDays,
            dayindex:currentDayIndex === 0 ? 6 : currentDayIndex - 1,
            day:currentDate.getDate()
        })
      },




});
